package com.gaolang.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gaolang.dao.ManagerMapper;
import com.gaolang.dao.MedicineMapper;
import com.gaolang.dao.OrderFormMapper;
import com.gaolang.dao.UserMapper;
import com.gaolang.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.*;

/**
 * @author GAOLANG
 */
@Service
public class ManagerServiceImpl implements ManagerService{

    @Autowired
    ManagerMapper managerMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    MedicineMapper medicineMapper;

    @Autowired
    OrderFormMapper orderFormMapper;


    @Override
    public boolean loginAuthentication(Manager manager) {

        HashMap<String, Object> map = new HashMap<>();
        map.put("username",manager.getUsername());
        List<Manager> managers = managerMapper.selectByMap(map);
        for (Manager manager1 : managers) {
            if (manager1.getPassword().equals(manager.getPassword())){
                return true;
            }
        }

        return false;
    }

    @Override
    public List<User> selectAllUser() {
        return userMapper.selectList(null);
    }

    @Override
    public int deleteUserById(int id) {
        return userMapper.deleteById(id);
    }

    @Override
    public List<Integer> userPage() {
        Page<User> page = new Page<>(1, 9);
        userMapper.selectPage(page, null);
        List<Integer> pages = new ArrayList<>();

        for (int i = 1; i <= page.getPages(); i++) {
            pages.add(i);
        }

        return pages;
    }

    @Override
    public List<User> userByPage(int page) {
        Page<User> page2 = new Page<>(page, 9);
        userMapper.selectPage(page2, null);
        return page2.getRecords();
    }

    @Override
    public int userDeleteSome(ArrayList<Integer> list) {
        return userMapper.deleteBatchIds(list);
    }

    @Override
    public List<User> selectThree(String id, String username, String address) {
        if (id == null){
            id = "";
        }
        if (username == null){
            username = "";
        }
        if (address == null){
            address = "";
        }
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper
                .like("username",username)
                .like("id",id)
                .like("address",address);

        List<Map<String, Object>> maps = userMapper.selectMaps(wrapper);
        List<User> userList = new ArrayList<>();
        for (Map<String, Object> map : maps) {
            User user = new User((int)map.get("id"),(String) map.get("username"),(String) map.get("password"),(String) map.get("sex"),(String) map.get("address"),(String) map.get("tel"),(double)map.get("balance"));
            userList.add(user);
        }
        return userList;
    }

    @Override
    public List<Medicine> selectAllMedicine() {
        return medicineMapper.selectList(null);
    }

    @Override
    public List<Integer> medicinePage() {
        Page<Medicine> page = new Page<>(1, 9);
        medicineMapper.selectPage(page, null);
        List<Integer> pages = new ArrayList<>();

        for (int i = 1; i <= page.getPages(); i++) {
            pages.add(i);
        }

        return pages;
    }

    @Override
    public List<Medicine> medicineByPage(int page) {
        Page<Medicine> page2 = new Page<>(page, 9);
        medicineMapper.selectPage(page2, null);
        return page2.getRecords();
    }

    @Override
    public int deleteMedicineById(int id) {
        return medicineMapper.deleteById(id);
    }

    @Override
    public int medicineDeleteSome(ArrayList<Integer> list) {
        return medicineMapper.deleteBatchIds(list);
    }

    @Override
    public int addMedicine(Medicine medicine) {

        return medicineMapper.insert(medicine);
    }

    @Override
    public int modifyMedicine(Medicine medicine) {
        return  medicineMapper.updateById(medicine);
    }

    @Override
    public List<Medicine> selectMedThree(String id, String name, String type) {
        if (id == null){
            id = "";
        }
        if (name == null){
            name = "";
        }
        if (type == null){
            type = "";
        }
        QueryWrapper<Medicine> wrapper = new QueryWrapper<>();
        wrapper
                .like("name",name)
                .like("id",id)
                .like("type",type);

        List<Map<String, Object>> maps = medicineMapper.selectMaps(wrapper);
        List<Medicine> medicines = new ArrayList<>();
        for (Map<String, Object> map : maps) {
            Medicine medicine = new Medicine((int)map.get("id"),(String) map.get("name"),(String) map.get("specif"),(String) map.get("type"),(double) map.get("price"),(Date) map.get("period_of_validity"),(String) map.get("image"),(String) map.get("introduce"),(Integer) map.get("amount"),(Integer)map.get("deleted"));
            medicines.add(medicine);
        }
        return medicines;
    }

    @Override
    public List<Integer> orderPage() {
        Page<OrderForm> page = new Page<>(1, 9);
        orderFormMapper.selectPage(page, null);
        List<Integer> pages = new ArrayList<>();

        for (int i = 1; i <= page.getPages(); i++) {
            pages.add(i);
        }

        return pages;
    }

    @Override
    public List<Orders> orderByPage(int page) {
        Page<OrderForm> page2 = new Page<>(page, 9);
        orderFormMapper.selectPage(page2, null);
        List<OrderForm> orderForms =  page2.getRecords();
        ArrayList<Orders> orders = new ArrayList<>();
        for (int i = 0; i < orderForms.size(); i++) {
            Medicine medicine = medicineMapper.selectById(orderForms.get(i).getMedId());
            User user = userMapper.selectById(orderForms.get(i).getUserId());
            orders.add(new Orders(orderForms.get(i).getId(),
                    orderForms.get(i).getState(),
                    orderForms.get(i).getMedId(),
                    orderForms.get(i).getUserId(),
                    orderForms.get(i).getQuantity(),
                    medicine.getName(),user.getUsername(),medicine.getPrice()*orderForms.get(i).getQuantity()));
        }
        return orders;
    }

    @Override
    public List<Orders> selectByUserId(Integer userId) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("user_id",userId);
        List<OrderForm> orderForms = orderFormMapper.selectByMap(map);
        ArrayList<Orders> orders = new ArrayList<>();
        for (int i = 0; i < orderForms.size(); i++) {
            Medicine medicine = medicineMapper.selectById(orderForms.get(i).getMedId());
            User user = userMapper.selectById(orderForms.get(i).getUserId());
            orders.add(new Orders(orderForms.get(i).getId(),
                    orderForms.get(i).getState(),
                    orderForms.get(i).getMedId(),
                    orderForms.get(i).getUserId(),
                    orderForms.get(i).getQuantity(),
                    medicine.getName(),user.getUsername(),medicine.getPrice()*orderForms.get(i).getQuantity()));
        }
        return orders;
    }

    @Override
    public Orders selectById(Integer id) {
        OrderForm orderForm = orderFormMapper.selectById(id);
        Medicine medicine = medicineMapper.selectById(orderForm.getMedId());
        User user = userMapper.selectById(orderForm.getUserId());
        Orders orders = new Orders(orderForm.getId(),orderForm.getState(),orderForm.getMedId(),orderForm.getUserId(),orderForm.getQuantity(),medicine.getName(),user.getUsername(),medicine.getPrice()*orderForm.getQuantity());
        return orders;
    }

    @Override
    public int modifyStateById(Integer id,String state) {

        OrderForm orderForm = orderFormMapper.selectById(id);
        orderForm.setState(state);
        return orderFormMapper.updateById(orderForm);
    }


}
